package org.zjvis.datascience.web.init;

import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.zjvis.datascience.common.config.DatasourceConfig;
import org.zjvis.datascience.common.constant.Constant;
import org.zjvis.datascience.common.constant.DatabaseConstant;
import org.zjvis.datascience.common.dto.DatabaseDTO;
import org.zjvis.datascience.service.*;

import javax.servlet.ServletContext;

/**
 * @author AaronY
 * @version 1.0
 * @since 2022/4/13
 */
@Order(2)
@Configuration
public class InitDefaultDataSourceKey implements ApplicationRunner {

    private final static Logger logger = LoggerFactory.getLogger(InitDefaultDataSourceKey.class);

    private final String pg_identifier = DatasourceConfig.Driver.Postgres.name();

    @Value("${postgres.host}")
    private String gp_address;

    @Value("${postgres.port}")
    private String gp_port;

    @Value("${postgres.database}")
    private String db_name;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private SemanticService semanticService;

    @Autowired
    private FastTextService fastTextService;

    @Autowired
    private UrbanDataService urbanDataService;

    @Autowired
    private JhubService jhubService;

    @Autowired
    private DatabaseService databaseService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        DatabaseDTO databaseDTO = databaseService.queryById(DatabaseConstant.DEFAULT_DATASET_ID);
        String dataSourceKey = Joiner.on("_").join(databaseDTO.getJDBCDriver(), gp_address, gp_port, db_name);
        logger.info("set default data source key -> {}", dataSourceKey);
        servletContext.setAttribute(Constant.DEFAULT_DATA_SOURCE_KEY, dataSourceKey);
        fastTextService.getAutojoinModel();
        semanticService.init();
        urbanDataService.initAutoJoinModel();
        jhubService.init();

    }


}
